import request from '@/utils/request';
import { ApiResult } from '@/api';

/**
 * 获取回显数据
 */
export async function getConfig(params: any) {
  const res = await request.get<ApiResult<any>>('/setting/get', { params });
  if (res.data.code === 0 && res.data.data) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 提交表单
 * @param data
 */
export async function submitForm(data: any) {
  const res = await request.post<ApiResult<unknown>>('/setting/update', data);
  if (res.data.code === 0) {
    return res.data;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 上传资料
 * @param data
 */
export async function upload(data: any) {
  const res = await request.post<ApiResult<any>>('/setting/upload', data);
  if (res.data.code === 0) {
    return res.data;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 操作
 * @param field
 */
export async function clearData(field) {
  const res = await request.get<ApiResult<any>>('clear-cache', {
    params: { field }
  });
  if (res.data.code === 0) {
    return res.data.message;
  }
  return Promise.reject(new Error(res.data.message));
}
